package com.esm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.esm.model.User;
import com.esm.service.AddMsgService;
import com.esm.serviceimpl.AddMsgServiceImpl;

import net.sf.json.JSONObject;

/**
 * 
 * @author zyj
 *
 */
public class AddMsgController extends HttpServlet{
	AddMsgService am=new AddMsgServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf8");
		String lcontent=req.getParameter("lcontent");//留言内容
		int oid=Integer.parseInt(req.getParameter("oid"));//被留言者ID
		int isreturn=Integer.parseInt(req.getParameter("isreturn"));//是否为回复
		int state=Integer.parseInt(req.getParameter("state"));//是否为已读
		HttpSession session=req.getSession();
		User curUser=(User) session.getAttribute("currentUser");
		int uid=curUser.getUid();//留言者的ID
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String pushTime=df.format(new Date()); //留言时间
		boolean addFlag=am.addMsg(uid, lcontent, oid, pushTime, isreturn, state);
		boolean addMsgLgroup=false;
		if(addFlag){
			addMsgLgroup = am.addMsgGroup();
		}
		JSONObject j=new JSONObject();
		j.put("addFlag", addMsgLgroup);
		PrintWriter pw=resp.getWriter();
		pw.write(j.toString());
	}
}
